$(function () {
    //页面加载完成获取第一页数据
    queryAjaxMaterialList(1);
    $('body').on('click', '#submitFrom,#querySubmit', function () {
        //点击搜索查询从第一页开始查询
        queryAjaxMaterialList(1);
    }).on('click', '#material-list-context i.fa-eye', function () {
       var that =$(this);
        //"VIDEO":AUDIO ARTICLE LIVE
        var materialName =$(this).parents('tr:eq(0)').find('.materialName').text()
        var  typeKey = $(this).data('typekey');
        if(typeKey=="VIDEO"||typeKey=="AUDIO"){
            previewDemand(materialName,'<script src="https://v.96koo.net/static/video/js/creatVideoPlayer.js?width=99%&height=99%&videoId=' + $(this).data('playurl') + '"></script>');
        }else if(typeKey=="ARTICLE"){
            $.ajax({
               url:baseloaction+"/admin/course/material/findMaterialId",
                data:{id:$(this).parents('tr:eq(0)').data('id')},
                type: "post",
                dataType: "json",
                success: function (result) {
                    previewDemand(materialName,'<div style="margin: 12px">'+result.articleContext+'</div>');
                },
                error: function (error) {
                    icModale(0, '系统繁忙，请稍后再试');
                }

            });
        }else if(typeKey=="LIVE"){
            icModale(0, '直播不支持预览');
        }

    });
    /**
     * 获取素材列表页面
     * @param pageNo 当前页码
     */
    function queryAjaxMaterialList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction +'/admin/course/material/ajaxSelectMaterialList',//请求URL
            contextId: 'material-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxMaterialList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-material', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").html('(共'+total+'个素材)');
            }
        };
        ajaxGetListData(obj)
    }

    // 预览视频与音频
    function previewDemand(name, content) {
        parent.layer.open({
            type: 1,
            title: '<strong>' + name + '</strong><small class="ml10"></small>',
            shadeClose: true,
            shade: [0.3, '#000000'],
            area: ['1000px', '520px'],
            content: content
        });
    }
});


/**
 * 获取选中素材
 */
function getSelectMaterial() {
    var input = $('#tbody input.i-checks:checked');
    if(input.length==0){
        icModale(0,"请选择素材数据");
        return null;
    }
    //data-live-begin=${material['liveBeginTime']},data-live-end=${material['liveEndTime']}
    var result = null;
    if(selectType==null || $.trim(selectType)=='' || typeof  selectType =='undefined' || selectType=='radio'){
        result = input[0];
        var _m = $(input[0]);
        var id = _m.val();
        var summary = $("#article-summary-"+id).text();
        var context = $("#article-context-"+id).text();
        result = {
            'id':_m.val(),
            'name':_m.attr("title"),
            'videoDuration':_m.data("video-duration"),
            'liveBeginTime':_m.data('live-begin'),
            'liveEndTime':_m.data('live-end'),
            'typeKey':_m.data('typekey'),
            'articleSummary':summary,
            'articleContext':context
        };
    }else{
        result = new Array();
        $.each(input,function (index,_m) {
            _m = $(_m);
            var id = _m.val();
            var summary = $("#article-summary-"+id).html();
            var context = $("#article-context-"+id).html();
            var _material = {
                'id':_m.val(),
                'name':_m.attr("title"),
                'videoDuration':_m.data('video-duration'),
                'liveBeginTime':_m.data('live-begin'),
                'liveEndTime':_m.data('live-end'),
                'typeKey':_m.data('typekey'),
                'articleSummary':summary,
                'articleContext':context
            };
            result.push(_material);
        });
    }
    return result;
}
